﻿package  
{
	import Box2D.Collision.*;
	import Box2D.Common.Math.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Dynamics.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author ...
	 */
	public class terrain 
	{
		
		public var m_terrainBody:b2Body;
		public var m_terrainVertices:Array;
		public var m_mc:MovieClip = new MovieClip;
		
		public function terrain(vertices:Array, world:b2World) 
		{
			// Maintain our vertices
			m_terrainVertices = vertices;
			
			var chainDef:b2StaticEdgeChainDef = new b2StaticEdgeChainDef();
			chainDef.friction = 4.0;
			chainDef.density = 0.0;
			chainDef.restitution = 0.0;
			chainDef.isALoop = false;
			chainDef.vertexCount = vertices.length;
			
			for (var i:int = 0; i < vertices.length; i++)
			{
				chainDef.vertices.push(new b2Vec2(cartesian(vertices[i]).x / main.m_physScale, cartesian(vertices[i]).y / main.m_physScale));
			}
			
			world.CreateGroundShape(chainDef);
			world.GetGroundBody().SetUserData(this);
			
		}
		
		public function draw(screen:Sprite, cam:camera):void
		{
			screen.graphics.clear();
			screen.graphics.lineStyle(1, 0x000000);
			
			screen.graphics.moveTo(cartesian(m_terrainVertices[0]).toScreen(cam).x, cartesian(m_terrainVertices[0]).toScreen(cam).y);
			for (var i:int = 1; i < m_terrainVertices.length; i++)
			{
				screen.graphics.lineTo(cartesian(m_terrainVertices[i]).toScreen(cam).x, cartesian(m_terrainVertices[i]).toScreen(cam).y);
			}
			
			
			
		}
		
	}
	
}